package com.zlc.sykks.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/*
* 创建线程池，注入到Bean中
* */
@Configuration
public class ConstanceConfig implements WebMvcConfigurer {

    //规定线程池核心线程数：5
    private static final int CORE_POLL_SIZE = 5;
    //规定线程池最大线程数： 10
    private static final int MAX_POOL_SIZE = 10;
    //规定线程池持有的队列容量：100
    private static final int QUEUE_CAPACITY = 100;
    //规定线程池 线程等待时间：1L
    private static final Long KEEP_ALIVE_TIME = 1L;

    /*
    * 创建一个线程池
    * */
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(){
        return  new ThreadPoolExecutor(
                CORE_POLL_SIZE,      //设置线程池核心线程数
                MAX_POOL_SIZE,       //设置线程池最大线程数量
                KEEP_ALIVE_TIME,     //设置线程等待时间
                TimeUnit.SECONDS,    //设置线程等待时间单位
                new ArrayBlockingQueue<>(QUEUE_CAPACITY),//设置线程池使用ArrayBlockingQueue队列
                //设置线程池饱和政策:CallerRunsPolicy(),"该策略既不会抛弃任务，也不会抛出异常，而是将任务回推到调用者
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
    }
}
